//
//  HMBaseNavController.swift
//  sinaweibo
//
//  Created by lst on 16/5/21.
//  Copyright © 2016年 lst. All rights reserved.
//

import UIKit

class HMBaseNavController: UINavigationController,UIGestureRecognizerDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()

        self.interactivePopGestureRecognizer?.delegate = self
    }
    //重写push 方法
    //HMBaseNavController(rootViewController: vc) ==> 内部会进行push操作 ==> 压栈
    override func pushViewController(viewController: UIViewController, animated: Bool) {
        //变量名称 和 方法名称 最好不要重名
        //判断是否是顶级页面
        var backTitle = "返回"
        if childViewControllers.count > 0 {
            if childViewControllers.count == 1{
                //设置返回按钮的title = 顶级页面的title
                backTitle = childViewControllers.first?.title ?? "返回"
            }
            let back = UIBarButtonItem(imageName: "navigationbar_back_withtext", title: backTitle, target: self, action: #selector(popToFrontVC))
            viewController.navigationItem.leftBarButtonItem = back
            }
        super.pushViewController(viewController, animated: animated)
        
        
    }
    @objc private func popToFrontVC(){
        popViewControllerAnimated(true)
    }
  
    
    //实现手势协议方法
    //一般协议方法返回值 为 bool类型 表示 如果返回 true 就表示对象能够正常工作
    //如果返回false  对象就不能正常工作
    
    func gestureRecognizerShouldBegin(gestureRecognizer: UIGestureRecognizer) -> Bool {
   
        return childViewControllers.count > 1
    }
   
}
